package com.eyaomall.wxmall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.dao.CollectDao;
import com.eyaomall.wxmall.model.po.Collect;
import com.eyaomall.wxmall.service.CollectService;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.cmp.kit.Pages;
import com.lycheeframework.core.exception.ErrorCodeException;
/**
 * 收藏服务
 * @author yuhuafeng
 * @date 2016-9-7
 */
@Service
public class CollectServiceImpl implements CollectService {
	@Autowired
	private CollectDao collectDao;
	
	/**
	 * 增加收藏
	 */
	public Integer create(Collect collect){
		if(collect==null)
			throw new ErrorCodeException(BECode.global_900001); 
		Collect co=queryByGoodsIDAndMemberId(collect.getGoodsId(), collect.getMemberId());
		if(co!=null){
			throw new ErrorCodeException(BECode.collect_6100010); 
		}
		collectDao.create(collect);
		return collect.getId();
	}
	
	/**
	 * 返会我的收藏分页
	 */
	public Pages<?> page(Collect collect, PageHTTPObject object){
		return collectDao.page(collect, object);
	}
	
	
	/**
	 * 删除我的收藏
	 */
	public boolean deleteByMemberId(int id){
		return collectDao.deleteByMemberId(id);
	}
	
	/**
	 * 根据商品id和会员id返回收藏信息
	 */
	public Collect queryByGoodsIDAndMemberId(Integer goodsId,Integer memberid){
		if(goodsId==null||memberid==null)
			throw new ErrorCodeException(BECode.global_900001); 
		Collect vo=new Collect();
		vo.setGoodsId(goodsId);
		vo.setMemberId(memberid);
		return (Collect) collectDao.query("queryByGoodsIDAndMemberId", vo);
	}

	/**
	 * 批量删除收藏
	 * @param ids
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean bitchdelete(String ids) {
		String[] arg=ids.split(",");
		if(arg.length>0){
			for (String string : arg) {
				if(!deleteByMemberId(Integer.parseInt(string)))
					throw new ErrorCodeException(BECode.collect_6100011); 
			}
			return true;
		}
		return false;
	}
}
